//
//  XMNavigationViewController.m
//  XMKit
//
//  Created by chenming on 2018/5/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMNavigationViewController.h"

@interface XMNavigationViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *blurBackView;

@end

@implementation XMNavigationViewController

+ (void)initialize {
    // 设置为不透明
    //    [[UINavigationBar appearance] setTranslucent:NO];
    // 设置导航栏背景颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor colorWithHexString:@"333333" alpha:1]];
    
    
    // 设置导航栏标题文字颜色
    // 创建字典保存文字大小和颜色
    NSMutableDictionary * color = [NSMutableDictionary dictionary];
    color[NSFontAttributeName] = [UIFont systemFontOfSize:16];
    color[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"333333" alpha:1];;
    [[UINavigationBar appearance] setTitleTextAttributes:color];
    
}


- (UIView *)blurBackView {
    if (_blurBackView == nil) {
        _blurBackView = [UIView new];
        _blurBackView.frame = CGRectMake(0, -20, kSCREENWIDTH, 64);
        CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
        gradientLayer.frame = CGRectMake(0, 0, kSCREENWIDTH, 64);
        gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"0x040012" alpha:0.76].CGColor,(__bridge id)[UIColor colorWithHexString:@"0x040012" alpha:0.28].CGColor];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(0, 1.0);
        [_blurBackView.layer addSublayer:gradientLayer];
        _blurBackView.userInteractionEnabled = NO;
        _blurBackView.alpha = 0.5;
    }
    return _blurBackView;
}

- (void)layoutSubviews {
    if (self.blurBackView.superview) {
        [self.blurBackView.superview bringSubviewToFront:self.blurBackView];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    self.navigationBarHidden = NO; //右滑返回可用
    //    self.navigationBar.hidden = YES; // 隐藏导航栏
    
    id target = self.interactivePopGestureRecognizer.delegate;
    
    SEL action = NSSelectorFromString(@"handleNavigationTransition:");
    
    //设置返回手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:target action:action];
    
    pan.delegate = self;
    
    [self.view addGestureRecognizer:pan];
    
    self.interactivePopGestureRecognizer.enabled = NO;
    
    // 取消横竖屏
//    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}


//- (BOOL)shouldAutorotate {
//    return [self.topViewController shouldAutorotate];
//}
//
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return [self.topViewController supportedInterfaceOrientations];
//}
//
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    return [self.topViewController preferredInterfaceOrientationForPresentation];
//}

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.visibleViewController;
}

//- (void)deviceOrientationDidChange {
//    if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
//        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
//        [self orientationChange:NO];
//        //注意： UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight
//    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
//        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
//        [self orientationChange:YES];
//    }
//}
//
//- (void)orientationChange:(BOOL)landscapeRight {
//    CGFloat width = [UIScreen mainScreen].bounds.size.width;
//
//    CGFloat height = [UIScreen mainScreen].bounds.size.height;
//    if (landscapeRight) {
//        [UIView animateWithDuration:0.2f animations:^{
//            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
//            self.view.bounds = CGRectMake(0, 0, width, height);
//        }];
//    } else {
//        [UIView animateWithDuration:0.2f animations:^{
//            self.view.transform = CGAffineTransformMakeRotation(0);
//            self.view.bounds = CGRectMake(0, 0, width, height);
//        }];
//    }
//}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.childViewControllers.count >= 1) {
        return YES;
    } else {
        return NO;
    }
}

/** 重写push自定义返回按钮 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.childViewControllers.count > 0) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"navi_back"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
        [backButton sizeToFit];
        backButton.backgroundColor = [UIColor whiteColor];
        //        backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        // 让按钮的内容往左边偏移8
        backButton.contentEdgeInsets =UIEdgeInsetsMake(0, -8,0, 0);
        backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0);
        //修改导航栏item
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
        //隐藏tarbbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    // 这个方法才是真正执行跳转
    [super pushViewController:viewController animated:animated];
    
    // 修改tabBra的frame
//    CGRect frame = self.tabBarController.tabBar.frame;
//    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
//    self.tabBarController.tabBar.frame = frame;
}

- (void)backController:(UIButton *)sender {
    // 判断两种情况: push 和 present
    if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self popViewControllerAnimated:YES];
    }
}


// 重写popViewControllerAnimated,animation设置YES /** 解决tabbar早于返回显示 */
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
//    NSArray *tempArray = self.navigationController.viewControllers;
//    for(UIViewController *tempVC in tempArray) {
//        if ([tempVC isKindOfClass:[XMBaseViewController class]]) {
//            [self.navigationController popToViewController:tempVC animated:YES];
//        }
//    }
    return [super popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
